package com.bbdjt.weixin.web.action.doctor;


import org.apache.log4j.Logger;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.bbdjt.weixin.base.action.BaseAction;
import com.bbdjt.weixin.model.Doctor;
import com.bbdjt.weixin.model.DoctorQueryModel;
import com.bbdjt.weixin.service.DoctorService;
import com.bbdjt.weixin.web.action.patient.PatientAction;

/**
 * @项目名称: bbdjt
 * @类名称: DoctorAction
 * @类描述: Doctor表相关操作
 * @创建人: guoLiang
 * @创建时间: 2017-01-18 04:37:42
 * @修改人: 
 * @修改时间: 
 * @修改备注:
 * @version
 */
@Controller
public class DoctorAction extends BaseAction {
	
	@Autowired
	private DoctorService doctorService;

	private String page_list = "jsp/doctor/doctorindex";

	private String redirect_list = "redirect:r_doctor";
	
	private static Logger logger = Logger.getLogger(DoctorAction.class);
	
	
	// 分页查询
	@RequestMapping(value = "r_doctor")
	public ModelAndView getListPage(DoctorQueryModel doctor) {
		ModelAndView result = new ModelAndView(page_list);
		doctor.setOrderBy("id asc");
		List<Doctor> doctorList = doctorService.selectByDoctor(doctor);
		result.addObject("pageInfo", new PageInfo<Doctor>(doctorList));
		result.addObject("doctor", doctor);
		return result;
	}

	// 自定义分页查询 根据xml 分页查询
	@RequestMapping(value = "r_doctorXml")
	public ModelAndView getListPageXml(DoctorQueryModel doctor) {
		ModelAndView result = new ModelAndView(page_list);
		doctor.setOrderBy("id asc");
		PageInfo<DoctorQueryModel> pageInfo = doctorService.selectByDoctorQueryModel(doctor);
		List<DoctorQueryModel> doctorList = pageInfo.getList();
		result.addObject("pageInfo", new PageInfo<DoctorQueryModel>(doctorList));
		result.addObject("doctor", doctor);
		return result;
	}

	// 返回整个页面的 json 包括分页 也签 内容 标示 和 正文 内容 同上方法 只不过是返回json
	@RequestMapping(value = "r_doctorJson", produces = "application/json")
	public ModelAndView pageInfo(DoctorQueryModel doctor) {
		ModelAndView result = new ModelAndView();
		PageInfo<DoctorQueryModel> pageInfo = doctorService.selectByDoctorQueryModel(doctor);
		result.addObject("pageInfo", pageInfo);
		return result;
	}
	
	//ligerui table
	@ResponseBody
	@RequestMapping(value = "r_doctorLigerUi", produces="application/json;charset=UTF-8") 
	public String pageInfoLigerUi(DoctorQueryModel doctor) {
		doctor.setOrderBy(doctor.getSortname(),doctor.getSortorder());
		PageInfo<DoctorQueryModel> pageInfo = doctorService.selectByDoctorQueryModel(doctor);
		return super.toLigerUiSelectJson(pageInfo);
	}

	// 添加数据 添加或者修改数据页面 主键为id
	// 如果没有id 不查 如果有查询补上信息
	@RequestMapping(value = "v_doctor")
	public ModelAndView add(Doctor doctor) {
		ModelAndView result = new ModelAndView();
		if (doctor.getId() != null) {
			doctor = doctorService.selectByKey(doctor.getId());
		}
		result.addObject("doctor", doctor);
		return result;
	}

	// 如果有id就更新 没有 就插入
	@RequestMapping(value = "c_doctor")
	public ModelAndView save(Doctor doctor) {
		ModelAndView result = new ModelAndView(redirect_list);
		if (doctor.getId() != null) {
			doctorService.updateNotNull(doctor);
		} else {
			// 会保存null 值
			doctorService.insertSelective(doctor);
		}
		return result;
	}

	// 删除数据
	@RequestMapping("d_doctor")
	public String delete(Integer id) {
		doctorService.delete(id);
		return redirect_list;
	}
	
	//医生通过手机号登录
	@ResponseBody
	@RequestMapping(value = "loginDoctor", produces="application/json;charset=UTF-8")
	public String loginDoctor (Doctor doctor){
		if(doctor.getMobile() != null){
			doctor = doctorService.selectDoctor(doctor);
			if(doctor != null){
				return super.writeOK(doctor);
			}else{
				return super.error(501, "您输入的手机号码没有注册，请重新输入。");
			}
		}else{
			logger.info("手机号为空:" + doctor.getMobile());
			return super.lackError();
		}
	}
}